﻿using DynamicDescriptors;
using Newtonsoft.Json;
using Revit同步插件.HttpServer;
using Revit同步插件.UI;
using System;
using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Controls;
using System.Windows.Forms;

/**
 * reference msdn:Getting the Most Out of the .NET Framework PropertyGrid Control
 * https://github.com/MatthewKing/DynamicDescriptors
 **/
namespace Revit同步插件
{
  class 自定义属性查看器
  {
    [STAThread]
    static void Main()
    {
      TreeTestWin win2 = new TreeTestWin();
      win2.ShowDialog();
      return;
      //ServerStart();

      StartupWindow win = new StartupWindow();
      win.ShowDialog();
      return;
      // 先构造参数对象
      var instanceToBind = new 自定义属性1();

      // 通过第三方类库，增加属性修饰信息。
      var descriptor = DynamicDescriptor.CreateFromInstance(instanceToBind);
      descriptor.GetDynamicProperty("Name") // by property-name
          .SetDisplayName("名称")
          .SetDescription("第一个属性")
          .SetConverter(new 下拉列表示例()) // drop box sample
          .SetCategory("样例");

      descriptor.GetDynamicProperty((自定义属性1 x) => x.Height) // by expression
          .SetDisplayName("高度")
          .SetDescription("第2个属性")
          .SetCategory("样例");

      descriptor.GetDynamicProperty((自定义属性1 x) => x.Width).SetDisplayName("宽度").SetDescription("第3个属性").SetCategory("样例");

      instanceToBind.PropertyObject = descriptor;

      // 传递给页面
      var s = new OptionsDialog();
      s.SetObject(instanceToBind, descriptor);
      s.ShowDialog();

      // 取值更新
      // instanceToBind 随页面之变化而变化
    }

    static void ServerStart()
    {
      Uri baseAddress = new Uri("net.tcp://localhost:8000/");
      ServiceHost<InternalWallCreate> host = new ServiceHost<InternalWallCreate>(baseAddress);

      host.AddServiceEndpoint(typeof(IWallCreate), new NetTcpBinding(), "WallCreate");
      host.EnableMetadataExchange = true;

      host.Open();

      Console.WriteLine("...");

      var key = Console.ReadKey();
      while (key.Key != ConsoleKey.Q)
      {
        key = Console.ReadKey();
      }

      host.Close();

    }
  }


  /// <summary>
  /// 自定义属性示例
  /// </summary>
  public class 自定义属性1
  {
    public string Name { get; set; }

    public double Height { get; set; }

    public double Width { get; set; }

    public string Text { get; set; }

    public object PropertyObject { get; set; }
    /// <summary>
    /// 仅为方便查看变化
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
      return JsonConvert.SerializeObject(this);
    }
  }


  public class 下拉列表示例 : StringConverter
  {
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
      return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
      return new StandardValuesCollection(new string[] {
        "选项1",
        "选项2",
        "选项3",
        "选项..."
      });
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
      return false;
    }

  }

  public class OptionsDialog : System.Windows.Forms.Form
  {
    private System.Windows.Forms.PropertyGrid OptionsPropertyGrid;
    System.Windows.Forms.TextBox textBox;
    object realObj;
    public OptionsDialog()
    {
      OptionsPropertyGrid = new PropertyGrid();
      OptionsPropertyGrid.Size = new System.Drawing.Size(300, 250);
      OptionsPropertyGrid.Dock = DockStyle.Fill;

      textBox = new System.Windows.Forms.TextBox() { MinimumSize = new System.Drawing.Size(100, 25) };
      textBox.Dock = DockStyle.Bottom;


      this.Controls.Add(OptionsPropertyGrid);
      this.Controls.Add(textBox);
      this.Text = "属性面板";

      OptionsPropertyGrid.PropertyValueChanged += OptionsPropertyGrid_TextChanged;
    }

    private void OptionsPropertyGrid_TextChanged(object sender, EventArgs e)
    {
      textBox.Text = realObj?.ToString();
    }

    /// <summary>
    /// 设置当前值
    /// </summary>
    /// <param name="obj"></param>
    public void SetObject(object realObj, object proObj)
    {
      OptionsPropertyGrid.SelectedObject = proObj;
      this.realObj = realObj;
    }

  }




}
